package jt.classic.app.clgl.clsqd;
 
import jt.classic.app.clgl.clsqd.impl.IClsqdImpl;
import jt.classic.system.ISystem;
import jt.classic.system.SimpleManager;
import jt.classic.system.group.GroupManager;
import jt.classic.system.group.IGroup;
import jt.classic.system.module.IMenu;
import jt.classic.system.module.IModule;
import jt.classic.system.module.impl.IMenuImpl;
import jt.classic.system.module.impl.IModuleImpl;
import jt.classic.system.role.IRole;
import jt.classic.system.role.RoleManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.impl.IUserImpl;

import org.limp.basework.ProcessorException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBeanProcessor;
import org.limp.basework.Table;
import org.limp.basework.analyzer.SimpleBeanAnalyzer;
import org.limp.basework.analyzer.SimpleBeanAnalyzer4HSQ;
import org.limp.basework.analyzer.TableRegisterCenter;
import org.limp.basework.impl.SimpleBeanImpl;
import org.limp.basework.processor.SingleBeanProcessor;

import org.limp.mine.EncryptTool;

import java.sql.Connection;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


/**
 * 商品集合管理
 * @author 
 *
 */
public final class ClsqdManager extends SimpleManager {
    public ClsqdManager(Connection conn) {
        super(conn);
    }

    public ClsqdManager() {
        super();
    }

    /**
     * 根据商品ID得到商品实例
     *
     * @param id 商品ID
     * @return 商品内容
     */
    public Map getSpById(String id) {
        String sql = "SELECT * FROM APP_SHANGPIN WHERE SP_ID = " + id;

        Map info = this.trimmer.searchSingleData(sql);

        return info;
    }

    /**
     * 删除商品信息
     *
     * @param ids 商品ID
     */
    public void deleteS(String ids) {
        String[] str = ids.split(",");
        String sql = null;

        for (int i = 0; i < str.length; i++) {
            if (!str[i].equals("")) {
                sql = "UPDATE APP_SHANGPIN SET SP_STATUS=" + -1 + " where sp_id in (" + str[i] +")" ;
                this.trimmer.executeBatch(sql);
            }
        }
    }
    
    /**
     * 更细商品状态信息
     *
     * @param ids 商品ID
     * @param status 状态
     */
    public void updateS(String ids,String status) {
        String[] str = ids.split(",");
        String sql = null;
        sql = "UPDATE APP_SHANGPIN SET SP_STATUS=" + status + " where sp_id in (" + ids +")" ;

        this.trimmer.executeBatch(sql);
//        for (int i = 0; i < str.length; i++) {
//            if (!str[i].equals("")) {
//                sql = "UPDATE APP_SHANGPIN SET SP_STATUS=" + status + " where sp_id in" + str[i] ;
//
//                this.trimmer.executeBatch(sql);
//            }
//        }
    }
	/**
     * 更细商品状态信息
     *
     * @param ids 商品ID
     * @param status 状态
     * @param type 状态
     */
    public void updateS(String ids,String status,String type) {
        String[] str = ids.split(",");
        String sql = null;
        String sqlspzh =  " SELECT  distinct T1.spzh_id         " +
        " FROM                                                                                        " +
        " app_spzh AS t1                                                                             " +
        " Inner Join app_spzh_m_clsqd AS t3 ON t1.SPZH_ID = t3.SPZH_ID                                " +
        " Inner Join  app_clsqd  AS t2 ON t1.SPZH_ID = t3.SPZH_ID AND t3.CLSQD_ID = t2.CLSQD_ID         " +
        " WHERE                                                                                       " +
        " t1.CLSQD_ID IN  (" + ids + ")                                                        " ;
        
        sql = "UPDATE APP_SPZH SET SPZH_STATUS=" + status + " where spzh_id in (" + sqlspzh +")" ;
        this.trimmer.executeBatch(sql);

    }   
    /**
	 * 根据id取得对象
	 * @param id
	 * @return
	 */
	public IClsqd getClsqdById(String id) {
		// TODO Auto-generated method stub
		 String sql = "SELECT t1.* FROM APP_CLSQD t1" +
		 		" WHERE CLSQD_ID = " + id;
	        Map info = this.trimmer.searchSingleData(sql);

	        if (info == null) {
	            return null;
	        }

	        IClsqd clsqd = new IClsqdImpl();

	        sql = " SELECT  t2.* ,t4.*  " +
	        " FROM                                                                                        " +
	        " app_clsqd AS t1                                                                             " +
	        " Inner Join app_clzp AS t3 ON t1.CLSQD_ID = t3.CLSQD_ID                                " +
	        " Inner Join  app_clxx  AS t2 ON t1.CLSQD_ID = t3.CLSQD_ID AND t3.CLXX_ID = t2.CLXX_ID         " +
	        " Inner Join  app_jsy  AS t4 ON t1.CLSQD_ID = t3.CLSQD_ID AND t3.JSY_ID = t4.JSY_ID         " +
	        " WHERE                                                                                       " +
	        " t1.CLSQD_ID =  '" + id + "'                                                        " ;

	        List list = this.trimmer.searchMultiData(sql);

//	        for (int i = 0; i < list.size(); i++) {
//	            Map temp = (Map) list.get(i);
//	            //temp.put("SPJHS", spzh);
//	            
//	            list.set(i, temp);
//	        }

	        info.put("CL_JSY", list);

	        clsqd.reload(info);

	        return clsqd;
	}
	/**
     * 批处理保存过程
     * @param tableName
     * @param counterName
     * @param request
     * @param conn
     * @return List {Map[ID=001],Map[ID=002]}
     * @throws ProcessorException
     */
    protected List manualSaveX(String tableName, String counterName,
        HttpServletRequest request, Connection conn,String key_value) throws ProcessorException {
        TableRegisterCenter center = TableRegisterCenter.getInstance();

        Table table = center.findTable(tableName);

        SimpleBeanAnalyzer sbAnalyzer = new SimpleBeanAnalyzer4HSQ(request);

        SimpleBean bean = sbAnalyzer.createMultiBean(table, counterName);

        List resultList = new ArrayList();

        if ((bean != null) && bean.getResource().containsKey(SimpleBean.KEY)) {
            SimpleBeanProcessor singleProcessor = new SingleBeanProcessor(this.conn);

            List list = (List) bean.getResource().get(SimpleBean.KEY);

            for (int i = 0; i < list.size(); i++) {
                SimpleBean _obj = (SimpleBean) list.get(i);
                _obj.getResource().remove("clsqd_id");
                _obj.getResource().put("clsqd_id", key_value);

                Map _map = (Map) singleProcessor.doSave(_obj, table);

                resultList.add(_map);
            }
        }

        return resultList;
    }
    
	/**
	 * 删除商品集合关联的商品
	 * @param clsqd_id
	 */
	public void delectCLZP(String clsqd_id) {
		// TODO Auto-generated method stub
		String sql = "delete from APP_CLZP WHERE CLSQD_ID = '" + clsqd_id +"'";
		this.trimmer.executeBatch(sql);
	}
	

}
